import java.util.ArrayList;

import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Combo;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.Group;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Listener;
import org.eclipse.swt.widgets.MessageBox;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;


public class ProjectLoader {
	Display display = new Display();
	Shell shell = new Shell(display, SWT.BORDER | SWT.CLOSE | SWT.MIN);

	Group comSQLServer = new Group(shell, SWT.NONE);
	Label lblSQLDomain = new Label(comSQLServer, SWT.HORIZONTAL);
	Text txtSQLDomain = new Text(comSQLServer, SWT.SINGLE | SWT.BORDER);
	Label lblSQLUsername = new Label(comSQLServer, SWT.HORIZONTAL);
	Text txtSQLUsername = new Text(comSQLServer, SWT.SINGLE | SWT.BORDER);
	Label lblSQLPassword = new Label(comSQLServer, SWT.HORIZONTAL);
	Text txtSQLPassword = new Text(comSQLServer, SWT.SINGLE | SWT.BORDER | SWT.PASSWORD);
	
	Button btnConnect = new Button(shell, SWT.PUSH);

	Combo cmbProjects = new Combo(shell, SWT.DROP_DOWN | SWT.READ_ONLY);
	Button btnLoad = new Button(shell, SWT.PUSH);
	Button btnCancel = new Button(shell, SWT.PUSH);
	
	IssuePlayerProject ipp = null;

	private boolean cancelled = true;
	
	public ProjectLoader(IssuePlayerProject ippA) {
		ipp = ippA;
		
		shell.setText("Issue Player: Select Project");
		shell.setSize(200, 350);

		comSQLServer.setText("SQL Server");
		comSQLServer.setLocation(0, 0);
		comSQLServer.setSize(200, 210);
		
		lblSQLDomain.setText("Domain");
		lblSQLDomain.setSize(lblSQLDomain.computeSize(SWT.DEFAULT, SWT.DEFAULT));
		lblSQLDomain.setLocation(10, 25);

		txtSQLDomain.setText("999999999999999999");
		txtSQLDomain.setSize(txtSQLDomain.computeSize(SWT.DEFAULT, SWT.DEFAULT));
		txtSQLDomain.setLocation(20, 50);
		txtSQLDomain.setText("");

		lblSQLUsername.setText("Username");
		lblSQLUsername.setSize(lblSQLUsername.computeSize(SWT.DEFAULT, SWT.DEFAULT));
		lblSQLUsername.setLocation(10, 85);

		txtSQLUsername.setText("999999999999999999");
		txtSQLUsername.setSize(txtSQLUsername.computeSize(SWT.DEFAULT, SWT.DEFAULT));
		txtSQLUsername.setLocation(20, 110);
		txtSQLUsername.setText("");

		lblSQLPassword.setText("Password");
		lblSQLPassword.setSize(lblSQLPassword.computeSize(SWT.DEFAULT, SWT.DEFAULT));
		lblSQLPassword.setLocation(10, 145);

		txtSQLPassword.setText("999999999999999999");
		txtSQLPassword.setSize(txtSQLPassword.computeSize(SWT.DEFAULT, SWT.DEFAULT));
		txtSQLPassword.setLocation(20, 170);
		txtSQLPassword.setText("");

		btnConnect.setText("Connect");
		btnConnect.setSize(btnConnect.computeSize(SWT.DEFAULT+100, SWT.DEFAULT));
		btnConnect.setLocation(100-(btnConnect.getSize().x/2), 215);
		btnConnect.addListener(SWT.Selection, new Listener() {
			public void handleEvent(Event e) {
				clearProjectList();
				if(!ipp.openSQLConnection(txtSQLDomain.getText(), txtSQLUsername.getText(), txtSQLPassword.getText())) {
					MessageBox msgbox = new MessageBox(shell, SWT.OK);
					msgbox.setText("Error");
					msgbox.setMessage("Cannot connect to database.");
					msgbox.open();
					return;
				}
				populateProjectList();
			};
		});

		cmbProjects.setSize(185, 200);
		cmbProjects.setLocation(5, 250);
		cmbProjects.addListener(SWT.Selection, new Listener() {
			public void handleEvent(Event e) {
				if(cmbProjects.getSelectionIndex() == -1)
					btnLoad.setEnabled(false);
				else
					btnLoad.setEnabled(true);
			}
		});
		
		btnLoad.setText("Load");
		btnLoad.setSize(btnLoad.computeSize(SWT.DEFAULT+100, SWT.DEFAULT));
		btnLoad.setLocation(195-btnLoad.getSize().x, 285);
		btnLoad.addListener(SWT.Selection, new Listener() {
			public void handleEvent(Event e) {
				ipp.openProject(cmbProjects.getText());
				cancelled = false;
				shell.dispose();
			};
		});
		btnLoad.setEnabled(false);
		
		btnCancel.setText("Cancel");
		btnCancel.setSize(btnCancel.computeSize(SWT.DEFAULT+100, SWT.DEFAULT));
		btnCancel.setLocation(0, 285);
		btnCancel.addListener(SWT.Selection, new Listener() {
			public void handleEvent(Event e) {
				shell.dispose();
			};
		});

		shell.open();

		if(ipp.isSQLOpen())
			populateProjectList();

		while (!shell.isDisposed()) {
			if (!display.readAndDispatch())
				display.sleep();
		}
		display.dispose();
		
	}

	private void populateProjectList() {
		ArrayList<String> projects = ipp.getProjectList();
		if(projects.size() == 0)
			return;
		for(int i = 0; i < projects.size(); i++)
			cmbProjects.add(projects.get(i));
		cmbProjects.setText(cmbProjects.getItem(0));
		btnLoad.setEnabled(true);
	}
	
	private void clearProjectList() {
		cmbProjects.removeAll();
		btnLoad.setEnabled(false);		
	}
	
	public boolean isCancelled() {
		return cancelled;
	}
}
